package com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.impl;

import com.cicc.beergamevirtualsimulationsystem.common.webSocket.config.WebSocketMessageSender;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.JsonMessageHandler;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.Impl.CurrentDataJsonWebSocketMessage;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.Impl.SelectRoleJsonWebSocketMessage;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.Impl.SomeoneChoosesRoleJsonWebSocketMessage;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.WebSocketMessageTypeEnum;
import com.cicc.beergamevirtualsimulationsystem.entity.RoomInfoManager;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.List;
import java.util.Map;

@Component
public class SelectRoleJsonMessageHandler implements JsonMessageHandler<SelectRoleJsonWebSocketMessage> {
    @Override
    public void handle(WebSocketSession session, SelectRoleJsonWebSocketMessage message) {
        Object roomId = session.getAttributes().get("roomId");
        String id = session.getAttributes().get("userId").toString();
        if (roomId != null) {
            // TODO: 2022/5/25 通知房间内其他玩家
            try {
                RoomInfoManager.selectRole(roomId.toString(),id.toString(),message.getMessage());
                WebSocketMessageSender.sendMessageToRoom((String) roomId,new SomeoneChoosesRoleJsonWebSocketMessage());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
//            String roomInfo = RoomInfoManager.getRoomInfo(roomId.toString(),id);


        }
    }

    @Override
    public String type() {
        return WebSocketMessageTypeEnum.PLAYER_SELECT_ROLE.getValue();
    }

    @Override
    public Class<SelectRoleJsonWebSocketMessage> getMessageClass() {
        return SelectRoleJsonWebSocketMessage.class;
    }
}
